//    The page gives admin the functionality to View/add/update/delete Medical School        
//    The data will be used from the tbl_medicalschool table.                                
//    Author: Rupinder Saini                                                                    
//    Date: May 02,2006  


using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using SiteManager;
using System.Data.SqlClient;

public partial class Admin_Ad_ManageMedSchool : System.Web.UI.Page
{    // creating an instance of manager class
    Manager mgr = new Manager(true,"ConnectionString");
    protected void Page_Load(object sender, EventArgs e)
    {
        lblMsg.Text = "";
        if (!Page.IsPostBack)
        {
            grdmedsch.Visible = true;
            BindGridView();
        }
        foreach (GridViewRow rv in grdmedsch.Rows)
        {
            CheckBox chkRow = (CheckBox)rv.FindControl("RowLevelCheckBox");
            chkRow.Attributes.Add("onclick", "ChangeHeaderAsNeeded();");
            ClientScript.RegisterArrayDeclaration("CheckBoxIDs", String.Concat("'", chkRow.ClientID, "'"));
        }
        lbltotMedSch.Text = TotalMedSchool().ToString();
    }
    private int TotalMedSchool()
    {
        int CountRec;
        if (txtName.Text.ToString().Trim() == "")
        {
            string sql = "Select Count(id)  from tbl_MedicalSchool where isdelete=0";
            CountRec = (int)mgr.ExecuteScalarQuery(sql);
        }
        else
        {
            string sql = "select count (*) from tbl_MedicalSchool where MedicalSchool Like('%'+'" + txtName.Text.Trim() + "'+'%') and isdelete=0";
            CountRec = (int)mgr.ExecuteScalarQuery(sql);
        }
            return CountRec;
    }

    // to delete a medical school
    protected void grdmedsch_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        try
        {
            string id = grdmedsch.DataKeys[e.RowIndex].Value.ToString();
            string delSql = "update tbl_MedicalSchool set isdelete=1 where id=" + id;
            mgr.ExecuteScalarQuery(delSql);
            BindGridView();
            lbltotMedSch.Text = TotalMedSchool().ToString();
        }
        catch (Exception ex)
        {
            Response.Write(ex.ToString());
        }
    } 

    // Delete confirmation
    protected void grdmedsch_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.Header)
        {
            CheckBox chkBox = (CheckBox)e.Row.FindControl("HeaderLevelCheckBox");
            chkBox.Attributes.Add("onclick", "ChangeAllCheckBoxStates(this.checked);");
        }

        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            try
            {
                LinkButton lnk = (LinkButton)e.Row.FindControl("lnkDelete");
                lnk.Attributes.Add("onclick", "return confirm('Are you sure to delete this record?')");
            }
            catch (Exception es)
            {
                Response.Write(es.ToString());
            }
        }

    }

    // paging
    protected void grdmedsch_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grdmedsch.PageIndex = e.NewPageIndex;
        BindGridView();
    }

    #region general functions

    /// <summary>
    /// binds the grid view
    /// </summary>
    public void BindGridView()
    {
        try
        {
            SqlParameter[] sqlParam = new SqlParameter[1];

            sqlParam[0] = new SqlParameter("@MedicalSchoolName", SqlDbType.VarChar);
            //sqlParam[0].Size = 500;
            sqlParam[0].Value = txtName.Text.Trim();

            DataSet ds = mgr.ExecuteQuery("[dbo].[get_medicalschools]", sqlParam, "tblMedicalSchools");
            if (ds.Tables[0].Rows.Count > 0)
            {
                grdmedsch.DataSource = ds;
                grdmedsch.PageSize = Convert.ToInt32(Session["AdminPageSize"].ToString());
                grdmedsch.DataBind();
            }
            else
            {

                lblMsg.Text = "No Records found!!<br>Please click the Add Medical School link <br>on the left panel to add a new Medical School.";
                lblMsg.ForeColor = System.Drawing.Color.Red;
                grdmedsch.Visible = false;
                lblMsg.Visible = true;
            }
        }
        catch (Exception ex)
        {
            Response.Write(ex.ToString());
        }
    }
    #endregion
    protected void butSerach_Click(object sender, EventArgs e)
    {
        grdmedsch.Visible = true;
        BindGridView();
    }
    protected void grdmedsch_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Del")
        {
            foreach (GridViewRow rv in grdmedsch.Rows)
            {
                CheckBox chk = (CheckBox)rv.FindControl("RowLevelCheckBox");
                if (chk.Checked)
                {
                    string id = grdmedsch.DataKeys[rv.RowIndex].Value.ToString();
                    string delSql = "update tbl_MedicalSchool set isdelete=1 where id=" + id;
                    mgr.ExecuteScalarQuery(delSql);
                }
            }
            BindGridView();
            lbltotMedSch.Text = TotalMedSchool().ToString();
        }

    }
    //ADDED BY RANJEET TO DISPLY TOTAL HOSPITAL SEARCH CRITERIA  WISE
    private int TotalSearchHospital()
    {
        string sql = "select id,MedicalSchool,Rating   from tbl_MedicalSchool  where MedicalSchool Like('%'+'" + txtName.Text.Trim() + "'+'%') and   isdelete=0 ";
        int CountRec = (int)mgr.ExecuteScalarQuery(sql);
        return CountRec;
    }
}
